/*
 *  $TEAM_NAME Space Trader
 */
package views;


import java.awt.Color;
import java.awt.Dimension;

import java.text.NumberFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import models.Player;



/**
 * Shipyard Panel
 * @author twb
 * @version 1.0
 */
public class ShipyardPanel extends JPanel {
	/**
	 * 135
	 */
	private static final int ONETHIRTYFIVE = 135;
	
	/**
	 * 20
	 */
	private static final int TWENTY = 20;
	
	/**
	 * 40
	 */
	private static final int FORTY = 40;
	
	/**
	 * 10
	 */
	private static final int TEN = 10;
	
	/**
	 * 50
	 */
	private static final int FIFTY = 50;
	
	/**
	 * 60
	 */
	private static final int SIXTY = 60;
	
	/**
	 * field
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * field
	 */
	private final Player player;
    
	/**
	 * field
	 */
	private final JLabel playerCreditsField;
    
	/**
	 * field
	 */
	private final JScrollPane shipTableScroller; 

	/**
	 * field
	 */
	private final ShipTablePanel shipTable;

    /**
     * Constructs the ship yard panel
     * 
     * @param player The game's player
     */
    public ShipyardPanel(Player player) {
        super.setLayout(null);

        this.player = player; 

        playerCreditsField = new JLabel();

        shipTable = new ShipTablePanel(player);
        shipTableScroller = new JScrollPane(shipTable);         

        super.add(playerCreditsField);
        super.add(shipTable);
        super.add(shipTableScroller);

        update();
    }

    /**
     * Updates the ship yard panel by updating the rows and the credits
     * 
     */
    public final void update() {
        shipTable.update(); 
        final NumberFormat fmt = NumberFormat.getCurrencyInstance();
        playerCreditsField.setText("Credits:  " + fmt.format(player.getCredits()));
    }

    /**
     * Updates the bounds of ship yard panel subunits
     * 
     */
    public void layoutSubviews() {
        final Dimension size = getSize();

        playerCreditsField.setBounds(size.width - ONETHIRTYFIVE, 0, (size.width >> 1) - 
        		TWENTY, FORTY);

        shipTableScroller.setBounds(TEN, FIFTY, size.width - TWENTY, size.height - SIXTY);
        shipTable.setBackground(Color.BLACK);
        shipTable.setPreferredSize(
        		new Dimension(size.width - TWENTY, FORTY 
        				* shipTable.getNumShipViews()));
        shipTable.setBounds(TEN, FIFTY, size.width - TWENTY, FORTY * 
        		shipTable.getNumShipViews());
    }

    /**
     * Updates the bounds of ship yard panel
     * 
     * @param x X-Position
     * @param y Y-Position
     * @param width Width of the ship yard panel
     * @param height Height of the ship yard panel
     */
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        layoutSubviews();
    }
}
